using LuaInterface;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(LuaClient))]
public class LuaClientInspector : Editor
{
    public LuaClient luaClient => target as LuaClient;

    public string editorLuaPath = "";
    public string editorDoString = "";

    void OnEnable()
    {

    }

    public override void OnInspectorGUI()
    {
        LuaState luaState = LuaClient.GetMainState();
        if (luaState == null)
        {
            EditorGUILayout.LabelField("LuaState not running!");
            return;
        }

        EditorGUILayout.BeginVertical();

        EditorGUILayout.LabelField("Module (Only default naming)");
        editorLuaPath = EditorGUILayout.TextField(editorLuaPath);
        if (GUILayout.Button("Reload", GUILayout.Height(35)))
        {
            string moduleName = editorLuaPath.Contains(".") ? editorLuaPath.Substring(editorLuaPath.LastIndexOf('.') + 1) : editorLuaPath;
            Debug.Log("reload name = " + moduleName);
            luaState.DoString(string.Format("moduleNameToTables['{0}'] = nil; setGlobal('{0}', nil)", moduleName));
            luaState.ReLoad(editorLuaPath);
            luaState.DoString(string.Format("setGlobal('{0}', {0})", moduleName));
        }

        editorDoString = EditorGUILayout.TextArea(editorDoString, GUILayout.Height(75));
        if (GUILayout.Button("DoString", GUILayout.Height(35)))
        {
            luaState.DoString(editorDoString);
        }
        EditorGUILayout.EndVertical();
    }

}
